<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mod_salarypara extends MYAD_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('Mmod_salarypara','Salarypara');
        date_default_timezone_set("Asia/Ho_Chi_Minh");  
    }
    /**
     * @author Thanh NV        
     * @function index      
     * @This function return Salary parameter list  
     */ 
    function index(){
        $data['title'] = $this->lang->line('spr__Title');
        $data['show'] = "index";        
        $this->load->library('pagination');             
        $total_data = $this->Salarypara->count_all(); 
	$perpage	= 10;
        $data['pagination'] = $this->paginationData($total_data,$perpage,'/salary-parameter');
        $offset = ($this->uri->segment(2)=='') ? 0 : $this->uri->segment(2); 
	$data['data'] = $this->Salarypara->listAllPagination($perpage, $offset,'Id'); 
        $data['messsageError']=(count($data['data'])==0) ? $this->lang->line('CR_null') : "";
        $data['page']=$offset;
        $this->load->view('layout',$data);
    }
    /**
     * @author Thanh NV        
     * @function addSalaryParameter 
     * @This function add salary parameter
     */ 
    function addSalaryParameter(){    
        if($this->input->post('btnButton')){
                $_POST['txtApplyDate'] = mb_convert_kana($_POST['txtApplyDate'], 'a');
                $_POST['txtTrialPeriodRate'] = mb_convert_kana($_POST['txtTrialPeriodRate'], 'a');
                $_POST['txtWeekdayOvertimeRate'] = mb_convert_kana($_POST['txtWeekdayOvertimeRate'], 'a');
                $_POST['txtWeekdayLateOvertimeRate'] = mb_convert_kana($_POST['txtWeekdayLateOvertimeRate'], 'a');
                $_POST['txtWeekendOvertimeRate'] = mb_convert_kana($_POST['txtWeekendOvertimeRate'], 'a');
                $_POST['txtWeekendLateOvertimeRate'] = mb_convert_kana($_POST['txtWeekendLateOvertimeRate'], 'a');
                $_POST['txtHolidayOvertimeRate'] = mb_convert_kana($_POST['txtHolidayOvertimeRate'], 'a');
                $_POST['txtHolidayLateOvertimeRate'] = mb_convert_kana($_POST['txtHolidayLateOvertimeRate'], 'a');
                $_POST['txtSocialInsuranceRate'] = mb_convert_kana($_POST['txtSocialInsuranceRate'], 'a');
                $_POST['txtMedicalInsuranceRate'] = mb_convert_kana($_POST['txtMedicalInsuranceRate'], 'a');
                $_POST['txtUnemploymentInsuranceRate'] = mb_convert_kana($_POST['txtUnemploymentInsuranceRate'], 'a'); 
                $this->load->library('form_validation'); 
                //$_POST['txtTrialPeriodRate'] = mb_convert_kana($_POST['txtTrialPeriodRate'], 'a');
                $this->form_validation->set_rules('txtApplyDate', $this->lang->line('spr__Applydate'), 'trim|required|callback_validate_month');
                $this->form_validation->set_rules('txtTrialPeriodRate', $this->lang->line('spr__Trialperiodrate'), 'required|greater_than[0]|less_than[1]'); 
                $this->form_validation->set_rules('txtWeekdayOvertimeRate', $this->lang->line('spr__Weekdayovertimerate'), 'required|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtWeekdayLateOvertimeRate',$this->lang->line('spr__Weekdaylateovertimerate'), 'required|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtWeekendOvertimeRate',$this->lang->line('spr__Weekendovertimerate'), 'required|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtWeekendLateOvertimeRate', $this->lang->line('spr__Weekendlateovertimerate'), 'required|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtHolidayOvertimeRate',$this->lang->line('spr__Holidayovertimerate'), 'required|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtHolidayLateOvertimeRate',$this->lang->line('spr__Holidaylateovertimerate'), 'required|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtSocialInsuranceRate', $this->lang->line('spr__Socialinsurancerate'), 'required|greater_than[0]|less_than[0.5]'); 
                $this->form_validation->set_rules('txtMedicalInsuranceRate', $this->lang->line('spr__Medicalinsurancerate'), 'required|greater_than[0]|less_than[0.5]'); 
                $this->form_validation->set_rules('txtUnemploymentInsuranceRate', $this->lang->line('spr__Unemploymentinsurancerate'), 'required|greater_than[0]|less_than[0.5]'); 
                if($this->form_validation->run() == TRUE){  
                         $data=array(
                                'CompanyCode'=>$this->session->userdata('CompanyCode'),
                                'Ins_ID'=>$this->session->userdata('UserId'),
                                'Ins_Date'=>date("Y-m-d h:i:s"),
                                'Upd_ID'=>$this->session->userdata('UserId'),
                                'Upd_Date'=>date("Y-m-d h:i:s"),
                                'TrialPeriodRate'=> $this->input->post('txtTrialPeriodRate'),
                                'WeekdayOvertimeRate'=>$this->input->post('txtWeekdayOvertimeRate'),                
                                'WeekdayLateOvertimeRate'=>$this->input->post('txtWeekdayLateOvertimeRate'),
                                'WeekendOvertimeRate'=>$this->input->post('txtWeekendOvertimeRate'),
                                'WeekendLateOvertimeRate'=>$this->input->post('txtWeekendLateOvertimeRate'),
                                'HolidayOvertimeRate'=>$this->input->post('txtHolidayOvertimeRate'),
                                'HolidayLateOvertimeRate'=>$this->input->post('txtHolidayLateOvertimeRate'),
                                'SocialInsuranceRate'=>$this->input->post('txtSocialInsuranceRate'),
                                'MedicalInsuranceRate'=>$this->input->post('txtMedicalInsuranceRate'),
                                'UnemploymentInsuranceRate'=>$this->input->post('txtUnemploymentInsuranceRate'),
                                'ApplyDate'=>str_replace("/","-",$this->input->post('txtApplyDate')).'-01',
                                //'ApplyDate'=>date('Y-m-d',strtotime( $this->input->post('txtApplyDate'))),
                                 );
                        
                        //if($this->Salarypara->isApplyDate(date('Y-m-d',strtotime( $this->input->post('txtApplyDate'))))){
                         if($this->Salarypara->isApplyDate(str_replace("/","-",$this->input->post('txtApplyDate')).'-01')){
                                $result =$this->Salarypara->insertData($data);                            
                                if($result){                       
                                     $data= array(
                                            'flag' => TRUE,
                                            'message' => $this->lang->line('emp_updateDB_success')
                                        );
                                }else{
                                    $data=array(
                                                'flag' => FALSE,
                                                'message' => $this->lang->line('emp_updateDB_fall')
                                            );
                                    }
                        }else{
                                $data=array(
                                                'flag' => FALSE,
                                                'message' => $this->lang->line('MC012')
                                            );                    
                        } 
                        
               }   
                   // $_POST['txtTrialPeriodRate'] = mb_convert_kana($_POST['txtTrialPeriodRate'], 'A');
                    $data['TrialPeriodRate']= $this->input->post('txtTrialPeriodRate');
                    $data['WeekdayOvertimeRate']=$this->input->post('txtWeekdayOvertimeRate');        
                    $data['WeekdayLateOvertimeRate']=$this->input->post('txtWeekdayLateOvertimeRate');
                    $data['WeekendOvertimeRate']=$this->input->post('txtWeekendOvertimeRate');
                    $data['WeekendLateOvertimeRate']=$this->input->post('txtWeekendLateOvertimeRate');
                    $data['HolidayOvertimeRate']=$this->input->post('txtHolidayOvertimeRate');
                    $data['HolidayLateOvertimeRate']=$this->input->post('txtHolidayLateOvertimeRate');
                    $data['SocialInsuranceRate']=$this->input->post('txtSocialInsuranceRate');
                    $data['MedicalInsuranceRate']=$this->input->post('txtMedicalInsuranceRate');
                    $data['UnemploymentInsuranceRate']=$this->input->post('txtUnemploymentInsuranceRate');                        
                    $data['ApplyDate']=$this->input->post('txtApplyDate');
                
        }        
        $data['dialogTitle']=$this->lang->line('emp_dialog_title');
	$data['js'] = array('jquery-ui.js');
	$data['css'] = array('jquery-ui.css');		
        $data['title'] = $this->lang->line('spr__Title');
        $data['show'] = "add_salaryParameter";        
        $this->load->view('layout',$data);		
	
      }  
      /**
     * @author Thanh NV        
     * @function editSalaryParameter 
     * @This function edit salary parameter
     */ 
      function editSalaryParameter(){          //var_dump($this->input->post('chkDelete')); die;   
           $id= !empty($_POST['hidSalaryParameterID']) ? $_POST['hidSalaryParameterID'] : NULL;     
           
          /* check exists */ 
          if (!is_null($id)) {
                $data=$this->Salarypara->listId($id);
                if (!$data) {
                    redirect('/salary-parameter');
                }
           }elseif(is_null($id)){
                    redirect('/salary-parameter');               
           }
        
          if($this->input->post('btnButton')){ 
                $_POST['txtApplyDate'] = mb_convert_kana($_POST['txtApplyDate'], 'a');
                $_POST['txtTrialPeriodRate'] = mb_convert_kana($_POST['txtTrialPeriodRate'], 'a');
                $_POST['txtWeekdayOvertimeRate'] = mb_convert_kana($_POST['txtWeekdayOvertimeRate'], 'a');
                $_POST['txtWeekdayLateOvertimeRate'] = mb_convert_kana($_POST['txtWeekdayLateOvertimeRate'], 'a');
                $_POST['txtWeekendOvertimeRate'] = mb_convert_kana($_POST['txtWeekendOvertimeRate'], 'a');
                $_POST['txtWeekendLateOvertimeRate'] = mb_convert_kana($_POST['txtWeekendLateOvertimeRate'], 'a');
                $_POST['txtHolidayOvertimeRate'] = mb_convert_kana($_POST['txtHolidayOvertimeRate'], 'a');
                $_POST['txtHolidayLateOvertimeRate'] = mb_convert_kana($_POST['txtHolidayLateOvertimeRate'], 'a');
                $_POST['txtSocialInsuranceRate'] = mb_convert_kana($_POST['txtSocialInsuranceRate'], 'a');
                $_POST['txtMedicalInsuranceRate'] = mb_convert_kana($_POST['txtMedicalInsuranceRate'], 'a');
                $_POST['txtUnemploymentInsuranceRate'] = mb_convert_kana($_POST['txtUnemploymentInsuranceRate'], 'a');                
                
                $this->load->library('form_validation');
                $this->form_validation->set_rules('txtApplyDate', $this->lang->line('spr__Applydate'), 'trim|required|callback_validate_month');
                $this->form_validation->set_rules('txtTrialPeriodRate', $this->lang->line('spr__Trialperiodrate'), 'required|callback_validate_float|greater_than[0]|less_than[1]'); 
                $this->form_validation->set_rules('txtWeekdayOvertimeRate', $this->lang->line('spr__Weekdayovertimerate'), 'required|callback_validate_float|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtWeekdayLateOvertimeRate',$this->lang->line('spr__Weekdaylateovertimerate'), 'required|callback_validate_float|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtWeekendOvertimeRate',$this->lang->line('spr__Weekendovertimerate'), 'required|callback_validate_float|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtWeekendLateOvertimeRate', $this->lang->line('spr__Weekendlateovertimerate'), 'required|callback_validate_float|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtHolidayOvertimeRate',$this->lang->line('spr__Holidayovertimerate'), 'required|callback_validate_float|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtHolidayLateOvertimeRate',$this->lang->line('spr__Holidayovertimerate'), 'required|callback_validate_float|greater_than[0]|less_than[3]'); 
                $this->form_validation->set_rules('txtSocialInsuranceRate', $this->lang->line('spr__Socialinsurancerate'), 'required|callback_validate_float|greater_than[0]|less_than[0.5]'); 
                $this->form_validation->set_rules('txtMedicalInsuranceRate', $this->lang->line('spr__Medicalinsurancerate'), 'required|callback_validate_float|greater_than[0]|less_than[0.5]'); 
                $this->form_validation->set_rules('txtUnemploymentInsuranceRate', $this->lang->line('spr__Unemploymentinsurancerate'), 'required|callback_validate_float|greater_than[0]|less_than[0.5]');                 
                if($this->form_validation->run() == TRUE){ 
                         $data=array(
                        'CompanyCode'=>$this->session->userdata('CompanyCode'),   
                        'Upd_ID'=>$this->session->userdata('UserId'),
                        'Upd_Date'=>date("Y-m-d h:i:s"),    
                        'Del_Flag'=>($this->input->post('chkDelete')=='on') ? 1 : 0,
                        'TrialPeriodRate'=> $this->input->post('txtTrialPeriodRate'),
                        'WeekdayOvertimeRate'=>$this->input->post('txtWeekdayOvertimeRate'),                
                        'WeekdayLateOvertimeRate'=>$this->input->post('txtWeekdayLateOvertimeRate'),
                        'WeekendOvertimeRate'=>$this->input->post('txtWeekendOvertimeRate'),
                        'WeekendLateOvertimeRate'=>$this->input->post('txtWeekendLateOvertimeRate'),
                        'HolidayOvertimeRate'=>$this->input->post('txtHolidayOvertimeRate'),
                        'HolidayLateOvertimeRate'=>$this->input->post('txtHolidayLateOvertimeRate'),
                        'SocialInsuranceRate'=>$this->input->post('txtSocialInsuranceRate'),
                        'MedicalInsuranceRate'=>$this->input->post('txtMedicalInsuranceRate'),
                        'UnemploymentInsuranceRate'=>$this->input->post('txtUnemploymentInsuranceRate'),
//                       'ApplyDate'=>date('Y-m-d',strtotime( $this->input->post('txtApplyDate'))),
                         'ApplyDate'=>str_replace("/","-",$this->input->post('txtApplyDate')).'-01',
                    );
                            
                    if($this->Salarypara->checkApplyDate(str_replace("/","-",$this->input->post('txtApplyDate')).'-01',$id)){ 
                           $result =$this->Salarypara->updateData($data,$id);                           
                            if($result){                       
                                 $data= array(
                                        'flag' => TRUE,
                                        'message' => $this->lang->line('emp_updateDB_success')
                                    );
                            }else{
                                $data=array(
                                            'flag' => FALSE,
                                            'message' => $this->lang->line('emp_updateDB_fall')
                                        );
                                }
                     }else{
                         $data=array(
                                                'flag' => FALSE,
                                                'message' => $this->lang->line('MC012')
                                            ); 
                          } 
                $data['Del_Flag']=($this->input->post('chkDelete')=='on') ? 1 : 0;          
                }
                $data['TrialPeriodRate']= $this->input->post('txtTrialPeriodRate');
                $data['WeekdayOvertimeRate']=$this->input->post('txtWeekdayOvertimeRate');        
                $data['WeekdayLateOvertimeRate']=$this->input->post('txtWeekdayLateOvertimeRate');
                $data['WeekendOvertimeRate']=$this->input->post('txtWeekendOvertimeRate');
                $data['WeekendLateOvertimeRate']=$this->input->post('txtWeekendLateOvertimeRate');
                $data['HolidayOvertimeRate']=$this->input->post('txtHolidayOvertimeRate');
                $data['HolidayLateOvertimeRate']=$this->input->post('txtHolidayLateOvertimeRate');
                $data['SocialInsuranceRate']=$this->input->post('txtSocialInsuranceRate');
                $data['MedicalInsuranceRate']=$this->input->post('txtMedicalInsuranceRate');
                $data['UnemploymentInsuranceRate']=$this->input->post('txtUnemploymentInsuranceRate');                       
                $data['ApplyDate']=$this->input->post('txtApplyDate');
                $data['Id']=$this->input->post('hidSalaryParameterID');
        }else{
                $data=$this->Salarypara->listId($id);
         
        }
        $data['dialogTitle']=$this->lang->line('emp_dialog_title');
        $data['js'] = array('jquery-ui.js');
	$data['css'] = array('jquery-ui.css');		
        $data['title'] =$this->lang->line('spr__Title_Update');
        $data['show'] = "edit_salaryParameter";       
        $this->load->view('layout',$data);
      }
      
      
      
}
?>